package com.ruoyi.cs.base.supplier.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.base.supplier.domain.SupplierInfo;
import com.ruoyi.cs.base.supplier.service.ISupplierInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 供应商信息管理
 * @author Drj 2021/6/28
 */
@RestController
@RequestMapping("/cs/supplier")
@Api(value = "供应商信息管理模块", tags = "供应商信息管理接口")
public class SupplierInfoController extends BaseController {
    @Autowired
    private ISupplierInfoService supplierInfoService;

    /**
     * 获取供应商信息
     * @param supplierInfo
     * @return
     */
    @ApiOperationSupport(includeParameters = {"SupplierInfo.sortId", "SupplierInfo.supplierInfoId", "SupplierInfo.createTime"})
    @ApiOperation(value = "获取供应商信息列表",notes = "查询所有供应商信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SupplierInfo supplierInfo) {
        startPage();
        List<SupplierInfo> supplierInfos = supplierInfoService.selectSupplierInfoList(supplierInfo);
        return getDataTable(supplierInfos);
    }

    /**
     * 导出供应商信息
     *
     * @param supplierInfo
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody SupplierInfo supplierInfo) {
        List<SupplierInfo> supplierInfos = supplierInfoService.selectSupplierInfoList(supplierInfo);
        ExcelUtil<SupplierInfo> util = new ExcelUtil<SupplierInfo>(SupplierInfo.class);
        return util.exportExcel(supplierInfos, "供应商信息单数据");
    }

    /**
     * 新增供应商信息
     * @param supplierInfo 供应商信息单据
     * @return
     */
    @ApiOperation(value = "新增供应商信息", notes = "新增供应商信息")
    @ApiImplicitParam(name = "supplierInfo", value = "新增供应商信息", dataType = "SupplierInfo")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SupplierInfo supplierInfo) {
        try {
            supplierInfoService.insertSupplierInfo(supplierInfo);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新供应商信息
     * @param supplierInfo 供应商信息单据
     * @return
     */
    @ApiOperation(value = "更新供应商信息", notes = "更新供应商信息")
    @ApiImplicitParam(name = "supplierInfo", value = "更新供应商信息", dataType = "SupplierInfo")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SupplierInfo supplierInfo) {
        try {
            supplierInfoService.updateSupplierInfo(supplierInfo);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除供应商信息
     * 仅修改删除标志位
     *
     * @param supplierInfo 供应商信息单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody SupplierInfo supplierInfo) {

        return toAjax(supplierInfoService.updateSupplierInfoDeleteFlag(supplierInfo));
    }
}
